| Conditions | 1 |
| Paths | 1 |
| Total Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 16 | function elements($timeout, state, data, util, visibility) { |
||
| 17 | let ct = this; |
||
| 18 | ct.state = state; |
||
| 19 | ct.data = data; |
||
| 20 | ct.util = util; |
||
| 21 | ct.outcome = {}; |
||
| 22 | ct.keys = Object.keys; |
||
| 23 | ct.buyAmount = [1, 10, 25, 100, 1000]; |
||
| 24 | |||
| 25 | ct.getChance = function(element, player) { |
||
| 26 | let bonus = 1; |
||
| 27 | for (let resource of data.elements[element].includes) { |
||
| 28 | let allTime = player.statistics.all_time[resource]; |
||
| 29 | if (allTime) { |
||
| 30 | bonus *= allTime * data.constants.ELEMENT_CHANCE_BONUS + 1; |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | let singleChance = data.elements[element].abundance * bonus; |
||
| 35 | let chance = 1 - Math.pow(Math.max(0, 1 - singleChance), |
||
| 36 | Math.min(player.resources.dark_matter.number, ct.buyAmount[player.options.elementBuyIndex])); |
||
| 37 | |||
| 38 | return Math.min(1, chance); |
||
| 39 | }; |
||
| 40 | |||
| 41 | ct.buyElement = function(element, player) { |
||
| 42 | if (player.elements[element]) { |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | if (Math.random() < ct.getChance(element, player)) { |
||
| 46 | player.elements[element] = true; |
||
| 47 | player.exotic_upgrades[element] = {}; |
||
| 48 | for (let up in data.exotic_upgrades) { |
||
| 49 | player.exotic_upgrades[element][up] = false; |
||
| 50 | } |
||
| 51 | player.elements_unlocked++; |
||
| 52 | ct.outcome[element] = 'Success'; |
||
| 53 | } else { |
||
| 54 | ct.outcome[element] = 'Fail'; |
||
| 55 | } |
||
| 56 | player.resources.dark_matter.number -= Math.min(player.resources.dark_matter.number, ct.buyAmount[player.options.elementBuyIndex]); |
||
| 57 | |||
| 58 | util.delayedExec(performance.now(), performance.now(), 1000, () => ct.clearMessage(element)); |
||
|
|
|||
| 59 | }; |
||
| 60 | |||
| 61 | ct.clearMessage = function(element) { |
||
| 62 | ct.outcome[element] = ''; |
||
| 63 | }; |
||
| 64 | |||
| 65 | /* This function returns the class that determines on which |
||
| 66 | colour an element card */ |
||
| 67 | ct.elementClass = function(element, player) { |
||
| 68 | if (player.elements[element]) { |
||
| 69 | return 'element_purchased'; |
||
| 70 | } |
||
| 71 | if (isElementAvailable(element, player)) { |
||
| 72 | return 'element_available'; |
||
| 73 | } |
||
| 74 | return 'element_unavailable'; |
||
| 75 | }; |
||
| 76 | |||
| 77 | function isElementAvailable(element, player) { |
||
| 78 | for (let resource of data.elements[element].includes) { |
||
| 79 | if (player.resources[resource].unlocked) { |
||
| 80 | return true; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | |||
| 86 | /* This function returns the class that determines the secondary |
||
| 87 | colour of an element card */ |
||
| 88 | ct.elementSecondaryClass = function(element, player) { |
||
| 89 | return ct.elementClass(element, player) + '_dark'; |
||
| 90 | }; |
||
| 91 | |||
| 92 | ct.visibleTableElements = function(player) { |
||
| 93 | return visibility.visible(data.elements, isTableElementVisible, null, null, player); |
||
| 94 | }; |
||
| 95 | |||
| 96 | function isTableElementVisible(element, _, player) { |
||
| 97 | switch (ct.elementClass(element, player)) { |
||
| 98 | case 'element_purchased': |
||
| 99 | return !player.options.hideElementsPurchased; |
||
| 100 | case 'element_available': |
||
| 101 | return !player.options.hideElementsAvailable; |
||
| 102 | case 'element_unavailable': |
||
| 103 | return !player.options.hideElementsUnavailable; |
||
| 104 | default: |
||
| 105 | return true; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.